<?php

class PageSet
{
	private $noteset;
	private $bypage;
	private $itotal; // total of items
	private $table; // table used (notes,articles...)
	
	public $first = '';
	public $last = '';
	public $previous = '';
	public $next = '';
	public $total = 0;
	public $current = 0; // current : 1..2..3
	
	function __construct($noteset,$bypage,$table="notes")
	{
		$this->noteset = $noteset;
		$this->bypage = $bypage;
		$this->table = $table;
		
		$this->updateSet();
	}
	
	public static function printLoadMore($start,$limit)
	{
		echo "<div id=\"loadmore\" onclick=\"loadmore('$start','$limit')\">LOAD MORE</div>";
	}
	
	public function HTMLCombo()
	{
		$combo = "<select id=\"pagination_combo\" onchange=\"note_combo()\">";
		for($i = $this->total ; $i > 0 ; $i--)
			if($i == $this->total - $this->current +1)
				$combo .= "<option value=\"".$this->getLimit($i)."\" selected>$i/$this->total</option>";
			else
				$combo .= "<option value=\"".$this->getLimit($i)."\">$i/$this->total</option>";
		$combo .= "</select>";
		return $combo;
	}
	
	public function HTMLLink($page,$message='')
	{
		switch($page)
		{
			case 'current':
				if(empty($message)) $message = "this note";
				return "<span onclick=\"note_range(".$this->getLimit('current').")\">$message<span>";
			break;
			case 'next':
				if(empty($message)) $message = "&gt;";
				if($this->next < 0)
					 return "<span>&nbsp;</span>";
				else
					return "<span id=\"pagination_next\" onclick=\"note_range(".$this->getLimit('next').")\" class=\"highlightable\">$message</span>";
			break;
			case 'previous':
				if(empty($message)) $message = "&lt;";
				if($this->previous >= $this->itotal)
					return "<span>&nbsp;</span>";
				else
					return "<span id=\"pagination_previous\" onclick=\"note_range(".$this->getLimit('previous').")\" class=\"highlightable\">$message</span>";
			break;
			case 'first':
				if(empty($message)) $message = "|&lt;";
				if($this->current == $this->total)
					return "<span>&nbsp;&nbsp;</span>";
				else
					return "<span id=\"pagination_first\" onclick=\"note_range(".$this->getLimit('first').")\" class=\"highlightable\">$message</span>";
			break;
			case 'last':
				if(empty($message)) $message = "&gt;|";
				if($this->current <= 1)
					return "<span>&nbsp;&nbsp;</span>";
				else
					return "<span id=\"pagination_last\" onclick=\"note_range(".$this->getLimit('last').")\" class=\"highlightable\">$message</span>";
			break;
			default: return false; break;
		}
	}
	
	public function getLimit($page)
	{
		if(is_numeric($page))
		{
			$calc = ($this->total - $page) * $this->bypage;
			return  $calc. ',' . $this->bypage;
		}
		else switch($page)
		{
			case 'current':
				return $this->current.','.$this->bypage;
			break;
			case 'next':
				return $this->next.','.$this->bypage;
			break;
			case 'previous':
				return $this->previous.','.$this->bypage;
			break;
			case 'first':
				return $this->first.','.$this->bypage;
			break;
			case 'last':
				return $this->last.','.$this->bypage;
			break;
			default: return false; break;
		}
	}
	
	public function updateSet()
	{
		// id of the first and last notes in the db
		$db = new DataBase();
		$very_first = $db->getFirst('id',$this->table);
		$very_last = $db->getLast('id',$this->table);
		
		// number of notes in db
		$db->select($this->table);
		$this->itotal = $total = $db->num_rows();
		
		$bypage = $this->bypage;
		
		// total of pages
		$this->total = $ptotal = ceil($total / $bypage);
		
		// on screen...
		$first = $this->noteset->getAbsoluteFirstId();
		$last = $this->noteset->getAbsoluteLastId();

		// page number
		$fid = $total - $last + 1; // fake id, as if ordered ASC (1, 2, 3...)
		$this->current = $pnum = $ptotal - ceil($last / $bypage) + 1;

		// first note of the PREVIOUS page
		$this->previous = $pnum * $bypage;

		// first note of NEXT page
		$this->next = ($pnum - 2) * $bypage;

		// first note of the first page (1st note, 2nd note...)
		$this->first = ($ptotal - 1) * $bypage;

		// first note of the last page (last note, note before last note...)
		$this->last = 0;
	}
	
	
	
}
?>